<template>
  <transition name="slide">
    <div class="user-center">
      <div class="back" @click="back">
        <i class="icon-back"></i>
      </div>

      <div class="switches-wrapper">
        <switches
          @switch="switchItem"
          :switches="switches"
          :currentIndex="currentIndex"
        ></switches>
      </div>

      <div class="play-btn" ref="playBtn" @click="random">
        <i class="icon-play"></i>
        <span class="text">随机播放全部</span>
      </div>

      <div class="list-wrapper" ref="listWrapper">
        <scroll
          class="list-scroll"
          ref="favoriteList"
          v-if="currentIndex === 0"
          :data="favoriteList"
        >
          <div class="list-inner">
            <song-list :songs="favoriteList" @select="selectSong"></song-list>
          </div>
        </scroll>

        <scroll
          class="list-scroll"
          ref="playList"
          v-if="currentIndex === 1"
          :data="playHistory"
        >
          <div class="list-inner">
            <song-list :songs="playHistory" @select="selectSong"></song-list>
          </div>
        </scroll>
      </div>
    </div>
  </transition>
</template>


<script type="text/ecmascript-6">
import Switches from 'common/switches/switches'
import Scroll from 'common/scroll/scroll'
import SongList from 'common/song-list/song-list'
import Song from 'assets/js/song'
import { playlistMixin } from 'assets/js/mixin'
import { mapActions, mapGetters } from 'vuex'


export default {
  mixins: [playlistMixin],
  data() {
    return {
      switches: [
        {
          name: '我喜欢的'
        }, {
          name: '最近听的'
        }
      ],
      currentIndex: 0,
    }
  },

  computed: {
    ...mapGetters([
      'favoriteList',
      'playHistory'
    ])
  },

  methods: {
    ...mapActions([
      'insertSong',
      'randomPlay'
    ]),
    back() {
      this.$router.back()
    },
    switchItem(index) {
      this.currentIndex = index
    },

    random() {
      let list = this.currentIndex === 0 ? this.favoriteList : this.playHistory
      list = list.map(item => {
        return 'getLyric' in item ? song : new Song(item)
      })
      if (list.length === 0) {
        return
      }
      this.randomPlay({
        list
      })
    },


    selectSong(song) {
      song = 'getLyric' in song ? song : new Song(song)
      console.log(song);
      this.insertSong(song)
    },

    handlePlaylist(playlist) {
      const bottom = playlist.length > 0 ? '60px' : ''
      this.$refs.listWrapper.style.bottom = bottom
      this.$refs.favoriteList && this.$refs.favoriteList.refresh()
      this.$refs.playList && this.$refs.playList.refresh()
    }
  },
  components: {
    Switches,
    Scroll,
    SongList
  }
}

</script>


<style scoped lang="stylus" ref="stylesheet/stylus">
@import '~assets/stylus/variable';

.user-center {
  z-index: 100;
  position: fixed;
  top: 0;
  bottom: 0;
  width: 100%;
  background: $color-background;

  &.slide-enter-active, &.slide-leave-active {
    transition: all 0.3s;
  }

  &.slide-enter, &.slide-leave-to {
    transform: translate3d(100%, 0, 0);
  }

  .back {
    z-index: 50;
    position: absolute;
    top: 0;
    left: 6px;

    .icon-back {
      display: block;
      padding: 10px;
      font-size: $font-size-large-x;
      color: $color-theme;
    }
  }

  .switches-wrapper {
    margin: 10px 0 30px 0;
  }

  .play-btn {
    margin: 0 auto;
    padding: 7px 0;
    width: 135px;
    box-sizing: border-box;
    border: 1px solid $color-text-l;
    border-radius: 100px;
    text-align: center;
    color: $color-text-l;
    font-size: 0;

    .icon-play {
      display: inline-block;
      vertical-align: middle;
      margin-right: 6px;
      font-size: $font-size-medium-x;
    }

    .text {
      display: inline-block;
      vertical-align: middle;
      font-size: $font-size-small;
    }
  }

  .list-wrapper {
    position: absolute;
    top: 110px;
    bottom: 0;
    width: 100%;

    .list-scroll {
      height: 100%;
      overflow: hidden;

      .list-inner {
        padding: 20px 30px;
      }
    }
  }
}
</style>

